import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../config/app_colors.dart';
import '../../utils/currency_manager.dart';
import 'currency_selector_dialog.dart';

class JoyTopWidget extends StatefulWidget {
  final VoidCallback? onCurrencyChanged;
  
  const JoyTopWidget({super.key, this.onCurrencyChanged});

  @override
  State<JoyTopWidget> createState() => _JoyTopWidgetState();
}

class _JoyTopWidgetState extends State<JoyTopWidget> {
  String _currentCurrency = 'USD,\$';
  late CurrencyChangeListener _currencyChangeListener;

  @override
  void initState() {
    super.initState();
    _initializeCurrency();
    _setupCurrencyListener();
  }

  @override
  void dispose() {
    // 移除货币变化监听器
    CurrencyManager.instance.removeListener(_currencyChangeListener);
    super.dispose();
  }

  // 初始化货币管理器
  void _initializeCurrency() async {
    await CurrencyManager.instance.initialize();
    if (mounted) {
      setState(() {
        _currentCurrency = CurrencyManager.instance.formattedCurrencyDisplay;
      });
    }
  }

  // 🔥 设置货币变化监听器，实现多个 JoyTopWidget 实例的同步
  void _setupCurrencyListener() {
    _currencyChangeListener = (newCurrency, oldCurrency) {
      if (mounted) {
        setState(() {
          _currentCurrency = '${newCurrency.code},${newCurrency.symbol}';
        });
      }
    };
    CurrencyManager.instance.addListener(_currencyChangeListener);
  }

  // 显示货币选择器
  void _showCurrencySelector() {
    CurrencyDialogHelper.showCurrencySelector(
      context,
      onCurrencySelected: (currency) async {
        await CurrencyManager.instance.setCurrency(currency);
        // 🔥 不需要手动 setState，监听器会自动更新
        // setState(() {
        //   _currentCurrency = '${currency.code},${currency.symbol}';
        // });
        
        // 通知父组件货币已改变
        widget.onCurrencyChanged?.call();
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 1.h),
      decoration: BoxDecoration(
        color:  AppColors.backHomeYellowGround,
        // boxShadow: [
        //   BoxShadow(
        //     color: AppColors.shadow,
        //     offset: Offset(0, 2),
        //     blurRadius: 4,
        //   ),
        // ],
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          // 左侧品牌logo和名称
          Row(
            children: [
              Image.asset('images/icon_logo.png', width: 80.w, height: 30.h),
            ],
          ),
          // 右侧货币选择按钮
          GestureDetector(
            onTap: _showCurrencySelector,
            child: Container(
              padding: EdgeInsets.symmetric(
                horizontal: 8.w,
                vertical: 6.h,
              ),
              decoration: BoxDecoration(
                color: AppColors.background,
                borderRadius: BorderRadius.circular(20.r),
                border: Border.all(
                  color: AppColors.primaryVariant,
                  width: 1.5,
                ),
              ),
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    _currentCurrency,
                    style: TextStyle(
                      fontSize: 12.sp,
                      fontWeight: FontWeight.w600,
                      color: AppColors.primaryVariant,
                    ),
                  ),
                  SizedBox(width: 4.w),
                  Icon(
                    Icons.keyboard_arrow_down,
                    size: 16.sp,
                    color: AppColors.primaryVariant,
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}